﻿#pragma once
#include<stdio.h>
#include<vector>
#include<iostream>
#include"Iterator.h"

using namespace std;
namespace hash_bucket
{
	template <class K, class V, class KeyOfValue, class HF>
	struct HBIterator;

	template<class K>
	struct HashFunc
	{
		size_t operator()(const K& key)
		{
			return (size_t)key;
		}
	};

	// 哈希表中支持字符串的操作
	template<>
	struct HashFunc<string>
	{
		size_t operator()(const string& key)
		{
			size_t hash = 0;
			for (auto e : key)
			{
				hash *= 31;
				hash += e;
			}

			return hash;
		}
	};


	template<class T>
	struct HashNode
	{
		T _data;
		HashNode<T>* _next;
		HashNode(const T& data)
			:_data(data)
			, _next(nullptr)
		{}
	};

	inline unsigned long __stl_next_prime(unsigned long n)
	{
		// Note: assumes long is at least 32 bits.
		static const int __stl_num_primes = 28;
		static const unsigned long __stl_prime_list[__stl_num_primes] =//数组
		{
		   53, 97, 193, 389, 769,
		   1543, 3079, 6151, 12289, 24593,
		   49157, 98317, 196613, 393241, 786433,
		   1572869, 3145739, 6291469, 12582917, 25165843,
		   50331653, 100663319, 201326611, 402653189, 805306457,
		   1610612741, 3221225473, 4294967291
		};
		const unsigned long* first = __stl_prime_list;//首尾指针
		const unsigned long* last = __stl_prime_list + __stl_num_primes;

		const unsigned long* pos = lower_bound(first, last, n);
		return pos == last ? *(last - 1) : *pos;// 传出最接近n的素数
	}




	// K 为 T 中key的类型
	// T 可能是键值对，也可能是K
	// KeyOfT: 从T中提取key
	// Hash将key转化为整形，因为hash函数使用除留余数法
	template<class K, class T, class KeyOfT, class Hash= HashFunc<K>>
	class HashTable
	{
		template <class K, class V, class KeyOfValue, class HF>
		friend struct HBIterator;
	public:
		typedef HBIterator<K, T, KeyOfT, Hash> Iterator;
		typedef HashNode<T> Node;
	public:
		HashTable()
		{
			_tables.resize(10, nullptr);
		}

		// 哈希桶的销毁
		~HashTable()
		{
			for (int a = 0; a < _tables.size(); a++)
			{
				Node* cur = _tables[a];
				Node* temp = nullptr;
				while (cur != nullptr)
				{
					temp = cur->_next;
					delete cur;
					cur = temp;
				}
				_tables[a] = nullptr;
			}
		}

		// 插入值为data的元素，如果data存在也插入
		pair<Iterator,bool> Insert(const T& data)
		{
			if ((float)_n / _tables.size() >= 1)
			{
				vector<Node*> newtable((unsigned long)__stl_next_prime(_tables.size()),nullptr);
				_tables.swap(newtable);
				for (int i = 0; i < newtable.size(); i++)
				{
					if (newtable[i] != nullptr)
					{
						Node* cur = newtable[i];
						Node* temp = nullptr;
						while (cur != nullptr)
						{
							temp = cur->_next;
							Insert(cur->_data);
							cur = temp;
						}
					}
				}
			}

			Hash hashf;
			KeyOfT kot;
			size_t setp = hashf(kot(data)) % _tables.size();//hash函数
			Node* newnode = new Node(data);
			newnode->_next = _tables[setp];
			_tables[setp] = newnode;
			_n++;
			return {Iterator(newnode, this), true };
		}

		Iterator Find(const K& key)
		{
			Hash hashf;
			KeyOfT kot;
			int setp = hashf(key) % _tables.size();
			if (setp < 0)
				setp += (int)_tables.size();

			Node* temp = _tables[setp];
			while (temp != nullptr)
			{
				if (kot(temp->_data) == key)
					return temp;
				else
					temp = temp->_next;
			}
			return nullptr;
		}



		bool Erase(const K& key)
		{
			Hash hashf;
			KeyOfT kot;
			int setp = hashf(key) % _tables.size();
			if (setp < 0)
				setp += (int)_tables.size();

			Node* temp = _tables[setp];
			Node* pre = temp;

			while (temp != nullptr)
			{
				if (kot(temp->_data) == key)
				{
					if (pre == temp)
					{
						_tables[setp] = _tables[setp]->_next;
						delete temp;
					}
					else
					{
						pre->_next = temp->_next;
						delete temp;
					}
					--_n;
					return true;
				}
				else
				{
					pre = temp;
					temp = temp->_next;
				}
			}
			return false;
		}

		size_t Count(K key)
		{
			size_t counter;

		}

		void Print()
		{
			KeyOfT kot;
			for (int a = 0; a < _tables.size(); a++)
			{
				Node* i = _tables[a];
				if (i == nullptr)
					cout << "nullptr";
				while(i!=nullptr)
				{
					cout << kot(i->_data) << " -> ";
					i = i->_next;
				}
				cout<<endl;
			}
		}

		bool empty()
		{
			return _n == 0;
		}

		Iterator Begin()
		{
			for (int i = 0; i < _tables.size(); i++)
			{
				if (_tables[i])
				{
					return Iterator(_tables[i],this);
				}
			}
			return nullptr;
		}

		Iterator End()
		{
			return nullptr;
		}
		size_t Size()
		{
			return _n;
		}

	private:
		vector<Node*> _tables;  // 指针数组
		size_t _n = 0;			// 表中存储数据个数
	};
}